//
//  LZMyPersonInfoCommonTableViewCell.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/27.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyPersonInfoCommonTableViewCell.h"

#import "Masonry.h"

@implementation LZMyPersonInfoCommonTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andType:(LZ_MY_INFO_CELL_TYPE)type
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.type = type;
        
        [self configView];
        
        if (type == LZ_MY_INFO_CELL_TYPE_LINKACCOUNT) {
            [self addAutoLayoutToCellForLinkAccount];
        }
        else {
            [self addAutoLayoutToCell];
        }
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.headImageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subTitleLabel];
    [self.contentView addSubview:self.accessoryImageView];
    [self.contentView addSubview:self.line];
}

- (void)addAutoLayoutToCellForLinkAccount {

    WS(weakSelf);
    
    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(weakSelf.headImageView.mas_trailing).with.offset(10);
        make.centerY.mas_equalTo(weakSelf.centerY);
    }];
    
    [self.accessoryImageView makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.size.mas_equalTo(CGSizeMake(6, 12));
    }];
    
    [self.subTitleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.trailing.mas_equalTo(weakSelf.accessoryImageView.leading).with.offset(-10);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.5);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(1);
    }];
}


- (void)addAutoLayoutToCell {

    WS(weakSelf);
    
    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.size.mas_equalTo(CGSizeMake(65, 65));
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(10);
        make.centerY.mas_equalTo(weakSelf.centerY);
    }];
    
    [self.accessoryImageView makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.size.mas_equalTo(CGSizeMake(6, 12));
    }];
    
    [self.subTitleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_greaterThanOrEqualTo(weakSelf.titleLabel.trailing).with.offset(10);
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.trailing.mas_equalTo(weakSelf.accessoryImageView.leading).with.offset(-10);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.5);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self layoutIfNeeded];
    
    if (self.type == LZ_MY_INFO_CELL_TYPE_HEADIMAGE) {
        self.headImageView.hidden   = NO;
        self.titleLabel.hidden      = YES;
    }
    else if (self.type == LZ_MY_INFO_CELL_TYPE_TITLE) {
        self.headImageView.hidden   = YES;
        self.titleLabel.hidden      = NO;
    }
    else if (self.type == LZ_MY_INFO_CELL_TYPE_LINKACCOUNT) {
        self.headImageView.hidden   = NO;
        self.titleLabel.hidden      = NO;
    }
}

- (UIImageView *)headImageView {

    if (_headImageView == nil) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.hidden = YES;
        _headImageView.image = [UIImage imageNamed:@"lz_userinfo_headerimage"];
    }
    
    return _headImageView;
}

- (UILabel *)titleLabel {

    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor   = UIColorFromRGB(0x333333, 1);
        _titleLabel.font        = [UIFont systemFontOfSize:15];
        _titleLabel.hidden      = YES;
    }
    
    return _titleLabel;
}

- (UILabel *)subTitleLabel {

    if (_subTitleLabel == nil) {
        _subTitleLabel = [[UILabel alloc] init];
        _subTitleLabel.textColor        = UIColorFromRGB(0x999999, 1);
        _subTitleLabel.font             = [UIFont systemFontOfSize:13];
        _subTitleLabel.userInteractionEnabled = YES;
        
    }
    
    return _subTitleLabel;
}

- (UIImageView *)accessoryImageView {

    if (_accessoryImageView == nil) {
        _accessoryImageView = [[UIImageView alloc] init];
        _accessoryImageView.userInteractionEnabled = YES;
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_ar"];
        } else {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_en"];
        }
        
    }
    
    return _accessoryImageView;
}

- (UIView *)line {

    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

@end
